#![allow(dead_code)]
#![allow(unused_variables)]

use diesel::pg::PgConnection;
use diesel::prelude::*;

use crate::conf::config::DataBase;

pub mod db;

pub fn establish_connection(db: DataBase) -> PgConnection {
    // postgres://username:password@localhost/diesel_demo
    let database_url = format!(
        "postgres://{}:{}@{}/{}",
        db.username, db.password, db.host, db.database
    );
    PgConnection::establish(&database_url)
        .unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
}

#[cfg(test)]
mod iox {
    use crate::conf::config::DataBase;
    use crate::db::establish_connection;

    #[test]
    fn stdin() {
        let connection = establish_connection(DataBase {
            host: "192.168.8.20:5432".to_string(),
            username: "postgres".to_string(),
            password: "af12g3k4SHF".to_string(),
            database: "nail".to_string(),
        });
    }
}
// postgres://postgres:af12g3k4SHF@192.168.8.20:5432/nail
